<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Account/classes/PhpFox_ComponentFavorites.class.php, $Author:  $ */



/** Favorites
 *
 * Params
 * 1. oUser (mandatory) - current userr
 * 2. bIsOwnerView (optional) - is viewed by owner in account aread (false by default)
 *
 * @package module.account
 * @version $Id: PhpFox_ComponentFavorites.class.php 46 2007-12-14 23:12:21Z phpfoxsv $
 *
 */
class PhpFox_Mod_Account_ComponentFavorites extends BaseComponent
{

    /** Processing method
     */
	function process()
	{
	    $oAcc = $this->getParam('oUser');
	    assert(null != $oAcc);

	    $oSrvSec = &App::getModuleService('Account', 'Security');
	    /* @var $oSrvSec PhpFox_Mod_Account_ServiceSecurity */

	    $oCurrUser = $oSrvSec->getCurrentUser();

	    $bIsOwner = ($oCurrUser && ($oAcc->getId() == $oCurrUser->getId()));

	    $oReq = &App::getRequest();
	    $sMode = $oReq->get('mode', 'full');
	    $oSrvFavorites = $this->_oModule->getService('Favorites');
	    /* @var $oSrvFavorites PhpFox_Mod_Account_ServiceFavorites */

	    $sPerformed = '';    //performed action
        $bSuccess   = false; //performed action
        $oTargAcc   = null;  //subject account
	    switch($oReq->getAction())
	    {
	        // ----- change view mode -----
	        case 'change_mode':
	            if ($oCurrUser == $oAcc)
	            {
    		        $oUrl = &App::getUrl();
    		        $oUrl->setParam('mode', $sMode);
    		        App::forward($oUrl->getUrl());
	            }
	            break;

	        // ----- add new favorite -----
	        case 'add':
	            if ($bIsOwner)
	            {
    	            $iFavId = $oReq->getInt('id');
    	            if ($iFavId)
    	            {
        	            $oTargAcc = $this->_oModule->getItem('Account');
        	            $oTargAcc->load($iFavId);
        	            $oSrvAcc = &App::getModuleService('Account', 'Account');
        	            /* @var $oSrvAcc PhpFox_Mod_Account_ServiceAccount */
        	            if ($oSrvAcc->checkAccesToProfile($oTargAcc, $oCurrUser))
        	            {
        	                $sPerformed = 'added'; //performed action
            	            $bSuccess = $oSrvFavorites->addToFavorite($oAcc, $oTargAcc);
        	            }
        	            else
        	            {
        	                $this->addErrors($oSrvAcc->getErrors());
        	            }
    	            }
    	        }
	            break;
	    }


		if ( $iFavId = $oReq->get('userdelete') )
		{
	            if ($bIsOwner)
	            {
    	            if ($iFavId)
    	            {
    	                $oTargAcc = $this->_oModule->getItem('Account');
        	            $oTargAcc->load($iFavId);
        	            $sPerformed = 'deleted';
        	            $bSuccess = $oSrvFavorites->deleteFromFavorite($oAcc, $oTargAcc);
    	            }
    	        }
		}

	    $iPage = $oReq->getInt('page', 1);
    	$iPageSize = App::getParam('public.pagesize');
	    list($aFavorites, $iTotal) = $oSrvFavorites->getFavorites($oAcc, $iPage, $iPageSize);
	    $oPager = App::newObject('Pager', $iTotal, $iPage, $iPageSize);


	    $oSrvAcc = &$this->_oModule->getService('Account');
	    /* @var $oSrvAcc PhpFox_Mod_Account_ServiceAccount */
	    foreach($aFavorites as $k=>$v)
	    {
	        $aFavorites[$k]['stars'] = $oSrvAcc->getStarsAsArray($v['rating']);
	        $aFavorites[$k]['age']   = $oSrvAcc->getAge($v);
	    }

		$oTpl = &$this->_oModule->getTemplate();
		$oTpl->assign(array(
			'aFavorites'   => $aFavorites,
			'sUserPicUrl'  => App::getParam('user.pic.url'),
	        'aPaging'      => $oPager->getInfo(App::getUrl()),
			'sMode'        => $sMode,
            'iRowSize'     => $this->getParam('iRowSize', 3),
			'sPerformed'   => $sPerformed,
			'bSuccess'     => $bSuccess,
		    'aTargAcc'     => (isset($oTargAcc->aData) ? $oTargAcc->aData : ''),
		    'bIsOwner'     => $bIsOwner,
		    'bIsOwnerView' => $this->getParam('bIsOwnerView', false),
		    'bSimpleView'  => $this->getParam('bSimpleView', false),
		    'aCurrUser'    => $oCurrUser ? $oCurrUser->aData : null,
		    'aErrors'      => $this->getErrors(),

		));
		$srcHtml = $oTpl->fetch('Favorites.html');
		return $srcHtml;
	}

}
?>